package handler

import (
	"2302a/tiktok_business/api/request"
	"2302a/tiktok_business/api/resp"
	"2302a/tiktok_business/global"
	"2302a/tiktok_business/internal/dao"
	"2302a/tiktok_business/internal/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"regexp"
)

func AddComment(c *gin.Context) {
	var req request.AddComment
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, http.StatusUnauthorized, "参数信息错误", err.Error())
		return
	}
	regexp.MustCompile("").Find([]byte(req.Content))
	if len(req.Content) >= 200 {
		resp.Error(c, http.StatusUnauthorized, "评论内容需非空（长度 1-200 字）", false)
		return
	}
	regexp.MustCompile(`https\\:/\\/\\`).Find([]byte(req.Content))
	if len(req.Content) >= 20 {
		resp.Error(c, http.StatusUnauthorized, "禁止包含链接", false)
		return
	}
	var user model.Users
	userId := c.GetUint("user_id")
	global.DB.Find(userId, &user)
	fmt.Println(user.UserId)
	var video model.Videos
	global.DB.Find(&video)
	if video.VideoId == 0 {
		resp.Error(c, http.StatusUnauthorized, "视频不存在", false)
		return
	}
	comments := model.Comments{
		VideoId: video.VideoId,
		UserId:  userId,
		Content: req.Content,
	}
	dao.Create(&comments)

	resp.Success(c, "评论内容成功", comments)
}

func LikeComment(c *gin.Context) {
	var req request.LikeComment
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, http.StatusUnauthorized, "参数信息错误", err.Error())
		return
	}
	if req.LikeCount >= 2 {
		resp.Error(c, http.StatusUnauthorized, "点赞失败", false)
		return
	}
	var user model.Users
	userId := c.GetUint("user_id")
	global.DB.Find(userId, &user)
	dao.Updates(&model.Comments{CommentId: req.CommentId, LikeCount: req.LikeCount})
	dao.Updates(&model.CommentLikes{
		CommentId: req.CommentId,
		UserId:    userId,
	})
	resp.Success(c, "点赞成功", req.LikeCount)
}

func UpdateComment(c *gin.Context) {
	var req request.UpdateComment
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, http.StatusUnauthorized, "参数信息错误", err.Error())
		return
	}
	var user model.Users
	userId := c.GetUint("user_id")
	global.DB.Find(userId, &user)
	fmt.Println(user.UserId)
	var video model.Videos
	global.DB.Find(&video)
	if video.VideoId == 0 {
		resp.Error(c, http.StatusUnauthorized, "视频不存在", false)
		return
	}
	global.DB.Updates(&model.Videos{VideoId: video.VideoId, UserId: userId, Description: req.Content})
	resp.Success(c, "收到评论成功", video)
}
